package com.pmsystem.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pmsystem.pojo.Complaint;
import com.pmsystem.pojo.User;
import com.pmsystem.service.ComplaintService;
import com.pmsystem.util.BeanUtil;

@Controller
public class ComplaintController {

	@Autowired
	private ComplaintService complaintService;
	
	/**
	 * 
	 * <p>Title: proposeComplaint</p >  
	 * <p>Description: 业主投诉</p >  
	 * @author 泽强
	 * @param complaint
	 * @return
	 */
	@RequestMapping(value="user/complaint",method=RequestMethod.POST )
	@ResponseBody
	public String proposeComplaint(HttpSession session,@RequestParam Map<String , Object> map) {
		Complaint complaint = BeanUtil.parserToBean(BeanUtil.mapToMapWithoutNull(map),Complaint.class);
		User user = (User) session.getAttribute("user");
		int userId = user.getUserId();
		complaint.setUserId(userId);
		boolean flag=complaintService.addComplaint(complaint);
		if(flag) {
			return "success";
		}
		return "error";
	}
	
	
	/**
	 * 
	 * <p>Title: findOwnComplaint</p >  
	 * <p>Description: 业主查找自己的投诉</p >  
	 * @author 泽强
	 * @param session
	 * @return
	 */
	@RequestMapping(value="user/findOwnComplaint",method=RequestMethod.GET)
	@ResponseBody
	public Object findOwnComplaint(HttpSession session) {
		User user = (User) session.getAttribute("user");
		int userId = user.getUserId();		
		return complaintService.findByUserId(userId);
	}
	
	/**
	 * 
	 * <p>Title: findComplaint</p >  
	 * <p>Description: 按一定条件查询投诉，注意SQL语句</p >  
	 * @author 泽强
	 * @param map
	 * @return
	 */
	@RequestMapping(value="findComplaint",method=RequestMethod.GET)
	@ResponseBody
	public Object findComplaint(@RequestParam Map<String , Object> map) {
		List<Complaint> list = complaintService.findComplaint(map);
		return list;
	}
	
	/**
	 * 
	 * <p>Title: update</p >  
	 * <p>Description: 管理员处理投诉</p >  
	 * @author 泽强
	 * @param complaint
	 * @return
	 */
	@RequestMapping(value= "admin/updateComplaint",method=RequestMethod.POST)
	@ResponseBody
	public Object updateComplaint(@RequestParam Map<String , Object> map) {
		Complaint complaint = BeanUtil.parserToBean(BeanUtil.mapToMapWithoutNull(map),Complaint.class);
		boolean flag=complaintService.updateComplaint(complaint);
		if(flag) {
			return "success";
		}
		return "error";
	}
}
